#!/bin/bash -ex

[ -e panda ] || git clone --recursive https://github.com/panda-re/panda

# git includes submodules hence recursive

ctf-tools-pip install -U pycparser

git clone git://git.code.sf.net/p/libdwarf/code libdwarf-code

# move to ctftools virtual env
source ctf-tools-venv-activate

pushd libdwarf-code
./configure --enable-shared
make -j $(nproc)
mkdir -p ../include
mkdir -p ../lib
cp libdwarf/libdwarf.h ../include
cp libdwarf/dwarf.h ../include
cp libdwarf/libdwarf.so ../lib
popd

#sed -i -e "s|/usr/local|$PWD|" panda/build.sh
export QEMU_CFLAGS="-I $PWD/include"
export QEMU_CXXFLAGS="-I $PWD/include"
#export LDFLAGS="-L $PWD/lib"
mkdir -p panda/build
cd panda/build
../build.sh "$@"
cd ../../

# link binaries
mkdir -p bin
cd bin
ln -s ../panda/build/i386-softmmu/qemu-system-i386 ./panda-qemu-system-i386
ln -s ../panda/build/arm-softmmu/qemu-system-arm ./panda-qemu-system-arm
ln -s ../panda/build/x86_64-softmmu/qemu-system-x86_64 ./panda-qemu-system-x86_64
for i in ../panda/scripts/*.py
do
	ln -s $i ./panda-$(basename $i)
done
cd ..
